﻿namespace PayPal.AdaptivePayments
{
    using System.Web.WebPages.Scope;
    using PayPal.Platform.SDK;

    public static class ConvertCurrency
    {
        public static ConvertCurrencyResponse Execute(
                                                CurrencyType[] baseAmountList,
                                                string[] convertToCurrencyList,
                                                string language = "")
        {
            if (string.IsNullOrEmpty(language))
            {
                language = Profile.Language;
            }

            Validate(language);

            ConvertCurrencyRequest currencyRequest = new ConvertCurrencyRequest();
            ConvertCurrencyResponse currencyResponse = new ConvertCurrencyResponse();

            currencyRequest.baseAmountList = baseAmountList;
            currencyRequest.convertToCurrencyList = convertToCurrencyList;

            currencyRequest.requestEnvelope = new RequestEnvelope();
            currencyRequest.requestEnvelope.errorLanguage = Profile.Language;

            AdapativePayments ap = new AdapativePayments();
            ap.APIProfile = Profile.FNProfile;
            currencyResponse = ap.ConvertCurrency(currencyRequest);

            if (ap.LastError != null)
            {
                throw ap.LastError;
            }

            return currencyResponse;
        }

        private static void Validate(string language)
        {
            ValidationHelper.ValidateProfile(Profile.FNProfile);
            ValidationHelper.ValidateLanguage(language);
        }
    }
}
